import 'dart:collection';

import 'package:elevator/model/group_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../model/build_detail.dart';
import '../../../model/build_info.dart';
import '../../../model/lift_info.dart';
import '../../../model/user.dart';

class HomeState {

  var selectBuildInfo = BuildInfo().obs;

  var selectGroupInfo = GroupInfo().obs;

  var buildList = <BuildInfo>[].obs;

  var selectBuildPositionId = 0.obs;

  var selectGroupPositionId = 0.obs;

  int alermNotifCount = 0;

  late User user;

  List<LiftInfo> allLiftList = [];

  ///电梯信息 <key：buildId，value:电梯信息>
  Map<String,BuildInfo> buildInfoMap = HashMap();

  HomeState();

  selectIdBuildChanged(int index){
    if(buildList.length>selectBuildPositionId.value){
      buildList[selectBuildPositionId.value].selected = false;
      if(buildList[selectBuildPositionId.value].groupList.length>selectGroupPositionId.value){
        buildList[selectBuildPositionId.value].groupList[selectGroupPositionId.value].selected = false;
      }
    }
    if(buildList.length>index){
      selectBuildPositionId.value = index;
      buildList[selectBuildPositionId.value].selected = true;
      selectBuildInfo.value = buildList[selectBuildPositionId.value];
      selectIdGroupChanged(0);
    }
  }

  void selectIdGroupChanged(int index) {
    if(buildList.length>selectBuildPositionId.value){
      if(buildList[selectBuildPositionId.value].groupList.length>selectGroupPositionId.value){
        buildList[selectBuildPositionId.value].groupList[selectGroupPositionId.value].selected = false;
      }
      if(buildList[selectBuildPositionId.value].groupList.length>index){
        selectGroupPositionId.value = index;
        buildList[selectBuildPositionId.value].groupList[index].selected = true;
        selectGroupInfo.value =  buildList[selectBuildPositionId.value].groupList[index];
      }
    }
  }

  ///对比电梯状态是否更新
  void checkClearState() {
    DateTime now = DateTime.now();
    debugPrint("AAAA 检测电梯状态"+now.toString());
    if(null != buildInfoMap && buildInfoMap.isNotEmpty){
      for(BuildInfo buildInfo in buildInfoMap.values){
        if(null != buildInfo.liftInfoStateMaps && buildInfo.liftInfoStateMaps.isNotEmpty){
          for(LiftInfo liftInfo in buildInfo.liftInfoStateMaps.values){
            liftInfo.checkClearState(now);
          }
        }
      }
    }
  }

  loadBuildInfo() {
    if(null != buildInfoMap && buildInfoMap.isNotEmpty){
      buildList.value.clear();
      buildList.value.addAll(buildInfoMap.values);
      selectIdBuildChanged(0);
      debugPrint("建筑信息加载完成，共：${buildList.length}个楼盘");
    }
  }


}
